<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table">
				<!-- 自定义列 -->
				<template #column-code="{ scope }">
					<qrcode-vue :value="scope.row.imei" size="80" />
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />

	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "device-device",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { h, reactive, ref } from "vue";
import UserSelect from "/$/user/components/user-select.vue";
import QrcodeVue from "qrcode.vue";

const { service } = useCool();
const { t } = useI18n();


// 选项
const options = reactive({
	status: [
		{ label: t("在线"), value: 0, type: "success" },
		{ label: t("离线"), value: 1, type: "danger" }, 
	],
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("名称"),
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		{
			label: t("imei"),
			prop: "imei",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		// {
		// 	label: t("密码"),
		// 	prop: "password",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		{
			label: t("ICCID"),
			prop: "iccid",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		{
			label: t("预警手机号"),
			prop: "alertPhone",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		{
			label: t("入网时间"),
			prop: "networkTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" },
			},
			span: 24,
		},
		// {
		// 	label: t("客服电话"),
		// 	prop: "servicePhone",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		// {
		// 	label: t("常见问题"),
		// 	prop: "faq",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		// {
		// 	label: t("使用说明"),
		// 	prop: "instructions",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		{
			label: t("设备状态"),
			prop: "status",
			component: { name: "el-radio-group", options: options.status },
			value: 1,
			required: true,
		},
		{
			label: t("选择绑定用户"),
			prop: "userId",
			component: { vm: UserSelect },
		},
	],
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { label: t("代理商名称"), prop: "tenantName", minWidth: 120 },
		{ label: t("名称"), prop: "name", minWidth: 120 },
		{ label: t("imei"), prop: "imei", minWidth: 120 },
		// { label: t("密码"), prop: "password", minWidth: 120 },
		{ label: t("ICCID"), prop: "iccid", minWidth: 120 },
		// { label: t("预警手机号"), prop: "alertPhone", minWidth: 120 },
		// {
		// 	label: t("入网时间"),
		// 	prop: "networkTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		// { label: t("客服电话"), prop: "servicePhone", minWidth: 120 },
		// { label: t("常见问题"), prop: "faq", minWidth: 120 },
		// { label: t("使用说明"), prop: "instructions", minWidth: 120 },
		{
			label: t("设备状态"),
			prop: "status",
			minWidth: 120,
			dict: options.status,
		},
		// {
		// 	label: t("创建时间"),
		// 	prop: "createTime",
		// 	minWidth: 170,
		// 	sortable: "desc",
		// 	component: { name: "cl-date-text" },
		// },
		// {
		// 	label: t("更新时间"),
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		// 自定义
		{
			label: "二维码",
			prop: "code",
			minWidth: 120,
		},
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.device.device,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
